import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:math';
import 'package:flutter_note/note/const.dart';

abstract class BaseState<T extends StatefulWidget> extends State<T> {
  var scaffoldkey = GlobalKey<ScaffoldState>(); //在每个page必须要加入key: scaffoldkey
  bool isShowing = false;
  Animation<double> transAnimation; //透明动画
  AnimationController transAnimationController; //登录透明变化动画控制器
  double transValue = 1.0; //记录登录页面透明度
  AlertDialog dialog;

  transAnim(int y) {
    transAnimationController = new AnimationController(
        duration: const Duration(milliseconds: 1),
        vsync: new AnimatedListState());
    transAnimation = new Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(transAnimationController);
    transAnimation.addListener(() {
      setState(() {
        transValue = pow(transAnimation.value, y); //透明度变化
      });
    });
    transAnimationController.forward();
  }

  //提示语
  void showMessage(msg) {
    //Scaffold.of(context).showSnackBar(SnackBar(content: Text(msg)));
    /*showModalBottomSheet<Null>(
        context: context, builder: (BuildContext context) {
      return   Padding(
          padding: const EdgeInsets.all(24.0),
          child: new Text(
              msg,
              textAlign: TextAlign.center,
              style: new TextStyle(
                  color: Color(0xFF333333),
                  fontSize: 18.0
              )
          )
      );
    });*/
    scaffoldkey.currentState
        .removeCurrentSnackBar(reason: SnackBarClosedReason.remove);
    var snackBar = SnackBar(
        content: Text(
          msg,
          textAlign: TextAlign.center,
        ),
        duration: Duration(milliseconds: 1500),
        backgroundColor: color.green);
    scaffoldkey.currentState.showSnackBar(snackBar);
  }

  //提示语
  void show() {
    isShowing = true;
    showDialog(
        context: context,
        builder: (ctx) => dialog = AlertDialog(
              content: Container(
                width: dim.x80,
                height: dim.x80,
                child: Center(
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation(color.green),
                  ),
                ),
              ),
            ));
  }

  //结束提示语
  void dismiss() {
    if (isShowing && dialog != null) {
      Navigator.pop(context);
      dialog = null;
    }
  }

  //横向分割线
  Divider lineHorizontal(size) {
    return Divider(
      height: size,
      color: color.line,
    );
  }

  //打开新的页面,且关闭前面页面
  void startToPage(pageName) {
    Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (context) => (pageName)),
        (route) => route == null);
    print('startToPage => $pageName');
    //Navigator.of(context).pushAndRemoveUntil(pageRouteBuilder(pageName), (route) => route == null);
  }

  //打开新的页面,且不关闭
  void startPage(pageName) {
    //Navigator.of(context).push(pageRouteBuilder(pageName));
    Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return pageName;
    }));
  }

  //打开新的页面,且不关闭
  Future<Object> startPageForResult(pageName) async {
    //var ret = await Navigator.of(context).push(pageRouteBuilder(pageName));
    var ret = await Navigator.of(context).push(MaterialPageRoute(builder: (_) {
      return pageName;
    }));
    return ret;
  }

  PageRouteBuilder pageRouteBuilder(pageName) {
    return PageRouteBuilder(
      opaque: false,
      transitionDuration: const Duration(milliseconds: 400),
      pageBuilder: (BuildContext context, _, __) {
        return pageName;
      },
      transitionsBuilder: (_, Animation<double> animation, __, Widget child) {
        return SlideTransition(
            position: new Tween<Offset>(
              begin: const Offset(1.0, 0.0),
              end: const Offset(0.0, 0.0),
            ).animate(animation),
            child: child);
      },
    );
  }

  @override
  void initState() {
    print("$this=>initState()");
    super.initState();
  }

  @override
  void dispose() {
    print("$this=>dispose()");
    if (transAnimationController != null) {
      transAnimationController.dispose();
    }
    super.dispose();
  }
}
